import imgui
import imgui_font
import db_connector
# import5个不同的窗口
import all_table_window
import item_movement_window
import new_request_window
import view_repair_window
import view_request_window

var db = db_connector.start()
using imgui

system.file.remove("./imgui.ini")
var app=window_application(get_monitor_width(0),get_monitor_height(0),"四川大学物资管理系统")
# fonts
var font=add_font_extend_cn(imgui_font.source_han_sans, 32)
var large_font = add_font_extend_cn(imgui_font.source_han_sans, 54)
var tiny_font = add_font_extend_cn(imgui_font.source_han_sans,18)
# 全局保存用户账户
var account = "Ora"
var password = "123456"
var user_id = 0

# 控制和显示当前管理员身份的界面
var job = 0
var job_map = { "1":"校区管理员","2":"小组管理员"}.to_hash_map() # 利用字典存储

# -----控制窗口是否显示的状态变量----
# 操作窗口
var if_menu = false 
# 登录窗口
var if_login_window = true
var if_login_success = false
# 修改所有表格的信息——校区管理员
var if_all_table_window = false
# 审核通过小组管理员提交的信息——校区管理员
var if_view_request_window = false
# 物品修理窗口——小组管理员
var if_view_repair_window = false
# 提交新物品登记窗口--小组管理员
var if_new_request_window = false
# 同级物品移动窗口--小组管理员
var if_item_movement_window = false

function set_all_subwindow_close()
    if_all_table_window = false
    if_view_request_window = false
    if_view_repair_window = false
    if_new_request_window = false
    if_item_movement_window = false
end
# -----------------------------------


# 必要的显示图片
var scu_image =  load_bmp_image("images/sichuan.bmp")
var cov_image = load_bmp_image("images/cov.bmp")
var ins_image = load_bmp_image("images/ins.bmp")

# 向用户展示消息的地方
var pass_message = ""


set_font_scale(1.0) 
# 身份验证逻辑
function identity(account,pass)
    var stmt = db.prepare("select account, pass, job, uuid from all_user where account=? and pass=?")
    stmt.bind(0,account)
    stmt.bind(1,pass)
    var res = stmt.exec()
    user_id = res[0][3].data.to_number()
    system.out.println(res)
    if res.size == 0
        return 0
    else 
        job = res[0][2].data
        if res[0][2].data == "1"
            return 1
        else
            if res[0][2].data == "2"
                return 2
            end
        end
    end
end
# 登录成功图形界面
function login_success()
    if if_login_success
        open_popup("欢迎##popup登录")
    end
    if begin_popup_modal("欢迎##popup登录",if_login_success,{flags.no_move,flags.always_auto_resize})
        text("登录成功! 欢迎您，"+ account + "，为您转到"+ job_map[job] + "界面" )
        end_popup() 
    end
end
# 登录窗口图形界面
function login_window() 
    if if_login_window
        begin_window("物资管理系统登录",if_login_window,{flags.no_collapse,flags.no_move,flags.no_resize})
            var w = 580
            var h = 800
            set_window_size(vec2(w, h))
            set_window_pos(vec2((get_monitor_width(0)-w) / 2,(get_monitor_height(0)-h) / 2))
            text(" ")
            push_font(large_font)
            text("  欢迎使用四川大学物资管理系统")
            pop_font()
            separator()
            text(" ")
            text("                ")
            same_line()
            input_text("账户",account,30)
            text("                ")
            same_line()
            input_text_s("密码",password,30,{flags.password})
            text("                " + pass_message)
            text("                                                ")
            same_line()
            if button("登录系统##login")
                switch identity(account,password)
                    case 1
                        if_menu = true
                        if_login_window = false
                        if_login_success = true
                    end
                    case 2
                        if_menu = true
                        if_login_window = false
                        if_login_success = true
                    end
                    default
                        pass_message = "密码错误！"
                    end
                end
            end
            text(" ")
            text("                                   ") 
            same_line()
            image(scu_image,vec2(200,70))
            text(" ")
            text("                                     ") 
            same_line()
            image(cov_image,vec2(200,80))
            text(" ")
            text("                                               ") 
            same_line()
            image(ins_image,vec2(80,80))
            text("                                            ")
            same_line()  
            push_font(tiny_font)
            text("智锐科创计算机协会")
            pop_font()
        end_window()
    end
end
# 菜单栏
function menu()
    if if_menu
        begin_window(job_map[job] + "界面##admin", if_menu, {flags.no_collapse,flags.no_move,flags.no_resize})
        if button("增删查改所有表")
            set_all_subwindow_close()    # 关闭其他窗口 
            all_table_window.data.init() # 初始化数据
            if_all_table_window = true   # 点亮窗口
        end
        if button("通过待审核的信息")
            set_all_subwindow_close()
            view_request_window.data.init()
            if_view_request_window = true
        end
        if button("查看辖域内物品")
            set_all_subwindow_close()
            view_repair_window.data.init()
            if_view_repair_window = true
        end
        if button("提交新物品请求")
            set_all_subwindow_close()
            new_request_window.data.init()
            if_new_request_window = true
        end
        if button("向同级别移动物品")
            set_all_subwindow_close()
            item_movement_window.data.init()
            if_item_movement_window = true
        end
        end_window()
    end
end

# 窗口的渲染循环
while !app.is_closed()
    app.prepare()
        style_color_light()
        push_font(font)
        login_window()
        menu()
        # 根据条件显示对应窗口，下同
        if if_all_table_window
            all_table_window.mywindow(if_all_table_window)
        end
        if if_view_request_window
            view_request_window.mywindow(if_view_request_window)
        end
        if if_view_repair_window
            view_repair_window.mywindow(if_view_repair_window)
        end
        if if_new_request_window
            new_request_window.mywindow(if_new_request_window)
        end
        if if_item_movement_window
            item_movement_window.mywindow(if_item_movement_window)
        end
        login_success()
        pop_font()    
    app.render()
end